package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.bean.CategoryInfo;
import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class GridHolder extends BaseHolder<Object> {
	private ImageView ivImage1;
	private ImageView ivImage2;
	private ImageView ivImage3;
	private TextView tvTitle1;
	private TextView tvTitle2;
	private TextView tvTitle3;
	private DisplayImageOptions options;
	
	@Override
	public void initData() {
		
		Object data = getData();
		if(data instanceof CategoryInfo){
			CategoryInfo categoryInfo = (CategoryInfo)data;
			tvTitle1.setText(categoryInfo.name1);
			tvTitle2.setText(categoryInfo.name2);
			tvTitle3.setText(categoryInfo.name3);
			if(!TextUtils.isEmpty(categoryInfo.url1)){
				ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+categoryInfo.url1, ivImage1, options);
			}
			if(!TextUtils.isEmpty(categoryInfo.url2)){
				ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+categoryInfo.url2, ivImage2, options);
			}
			if(!TextUtils.isEmpty(categoryInfo.url3)){
				ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+categoryInfo.url3, ivImage3, options);
			}
		}
	}
	@Override
	public View initView() {
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
		
		View view = UIUtils.inflate(R.layout.layout_grid_item);
		ivImage1 = (ImageView) view.findViewById(R.id.iv_image1);
		ivImage2 = (ImageView)view.findViewById(R.id.iv_image2);
		ivImage3 = (ImageView)view.findViewById(R.id.iv_image3);
		
		tvTitle1 = (TextView) view.findViewById(R.id.tv_title1);
		tvTitle2 = (TextView) view.findViewById(R.id.tv_title2);
		tvTitle3 = (TextView) view.findViewById(R.id.tv_title3);
		return view;
	}
}
